import Store from '../store';
import { replaceHtml, transformRangeToAbsolute, openSelfModel } from '../utils/util';
import { modelHTML } from './constant';
import sheetmanage from './sheetmanage';
import menuButton from './menuButton';
import { checkProtectionNotEnable } from './protection';
import { jfrefreshgrid } from '../global/refresh';
import locale from '../locale/locale';
import { setcellvalue } from '../global/setdata';
import { getRangeAxis, getRangeWithFlatten, getRange, getTxtByRange } from '../global/api';
import { checkCellInRange, checkRangeCrossing } from "../utils/matrix";
import dataVerificationCtrl from './dataVerificationCtrl';


let isInitialViewTableModel = false;
let viewTable
function initialViewTableModel(title) {
    if (isInitialViewTableModel) {
        return;
    }

    isInitialViewTableModel = true;

    $("body").append(replaceHtml(modelHTML, {
        "id": "luckysheet-view-table",
        "addclass": "luckysheet-view-table",
        "title": title,
        "content": `
        <div style="position:relative;">
            <table id="viewTableId" class="cell-border stripe" >
            </table>
        </div>
        `,
        "botton": "",
        "style": "z-index:1011;width:500px;"
    }));


}

function creatViewTable() {
    if (viewTable) {
        viewTable.destroy();
        $('#viewTableId').empty();
    }
    let option = {
        searching: false,
        paging: false,
        "info": false,//左下角信息
        "language": { "sEmptyTable": "暂无数据", },
        scrollY: 200,
        "columns": [
            {
                title: '位置', data: "cellPosition",
                orderable: false,
                render: function (data, type, row, meta) {
                    let arr = data.split(',')
                    let a = getTxtByRange([{ row: [arr[0] * 1, arr[0] * 1], column: [arr[1] * 1, arr[1] * 1] }])
                    return a
                }
            },
            { title: '名称', orderable: false, data: "writeDataSourceName" },
            { title: '操作权限', orderable: false, data: "writeDataSourceId", width: 80 }
        ],
        columnDefs: [{
            "targets": [2],
            render: function (a, b, c, d) {
                return `<a class="btn btn-primary view-table-abtn-del" data-index=${d.row}>删除</a>`;
            }

        }],
        data: [
            ..._.cloneDeep(Store.dataSourceList)
        ]

    }
    viewTable = $('#viewTableId').DataTable(option);
    viewTable.$($(".view-table-abtn-del")).click(function (e) {
        Store.dataSourceList.splice($(e.target).attr('data-index'), 1);
        creatViewTable()
    })
}
// 来源查看
export function openViewTableModel() {


    initialViewTableModel("已设置来源");
    openSelfModel("luckysheet-view-table");
    creatViewTable()

}

// 标记查看
export function openMarkViewTableModel() {


    initialMarkViewTableModel("标记查看");
    openSelfModel("luckysheet-mark-view-table");
    creatViewTableMark()


}

let isInitialMarkViewTableModel = false;
let markViewTable
function initialMarkViewTableModel(title) {
    if (isInitialMarkViewTableModel) {
        return;
    }

    isInitialMarkViewTableModel = true;

    $("body").append(replaceHtml(modelHTML, {
        "id": "luckysheet-mark-view-table",
        "addclass": "luckysheet-mark-view-table",
        "title": title,
        "content": `
        <div style="position:relative;">
            <table id="markViewTableId" class="cell-border stripe" >
            </table>
        </div>
        `,
        "botton": "",
        "style": "z-index:1011;width:500px;"
    }));

    // initialPermissionsViewModelEvent();

}

function creatViewTableMark() {
    if (markViewTable) {
        markViewTable.destroy();
        $('#markViewTableId').empty();
    }
    let option = {
        searching: false,
        paging: false,
        "info": false,//左下角信息
        "language": { "sEmptyTable": "暂无数据", },
        scrollY: 200,
        "columns": [
            {
                title: '名称', data: "keyContent",
                orderable: false,
            },
            {
                title: '值', orderable: false,
                render: function (data, type, row, meta) {
                    return ''
                }
            },
            { title: '单位', orderable: false, data: "unitContent" },
            { title: '操作权限', orderable: false, width: 80 }
        ],
        columnDefs: [{
            "targets": [3],
            render: function (a, b, c, d) {
                return `<a class="btn btn-primary view-table-abtn-del-mak" data-index=${d.row}>删除</a>`;
            }

        }],
        data: [
            ..._.cloneDeep(Store.drawSettingList)
        ]

    }
    markViewTable = $('#markViewTableId').DataTable(option);
    markViewTable.$($(".view-table-abtn-del-mark")).click(function (e) {
        Store.drawSettingList.splice($(e.target).attr('data-index'), 1);
        creatViewTableMark()
    })
}

